package ${basePackage};

import ${beanPackage}.${className};
import ${daoPackage}.${className}Dao;
import gz.util.DateUtil;
import gz.util.R;
import java.util.*;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// ${cname}
@Service
public class ${className}Service {

    private static final Logger logger = LoggerFactory.getLogger(${className}Service.class);

    @Autowired
    private ${className}Dao ${humpName}Dao;

    public R save(String token,${className} ${humpName}) {

        ${humpName}.setCreateTime(DateUtil.now());
    	${humpName}.setUpdateTime(DateUtil.now());
        try{
        	${humpName}Dao.Save(${humpName});
        	return R.ok(200,"操作成功");
        }catch (Exception e){
			logger.error("程序异常",e);
			return R.error(197,"程序异常");
       }
    }

    public R update(String token,${className} ${humpName}) {

		${humpName}.setUpdateTime(DateUtil.now());
        try{
        	${humpName}Dao.Update(${humpName});
        	return R.ok(200,"操作成功");
        }catch (Exception e){
			logger.error("程序异常",e);
			return R.error(197,"程序异常");
       }
    }

    public R delete(${className} ${humpName}) {
        try{
        	${humpName}Dao.Remove(${humpName});
        	return R.ok(200,"操作成功");
        }catch (Exception e){
			logger.error("程序异常",e);
			return R.error(197,"程序异常");
       }
    }

    public R deletes(String id) {
    	String[] ids = id.split(",");
    	boolean flag = true;
    	for(String _id:ids) {
    		if(StringUtils.isEmpty(_id)) {
    			continue;
    		}
    		${className} ${humpName} = new ${className}();
    		${humpName}.setId(Integer.valueOf(_id));
    		try {
				${humpName}Dao.Remove(${humpName});
			} catch (Exception e) {
				logger.error("删除id>>"+_id+"异常",e);
				flag = false;
			}
    	}
    	return flag?R.ok(200,"操作成功"):R.error(197,"程序异常");
    }

    public R load(String id) {
		if (StringUtils.isEmpty(id)) {
			return R.error(198, "参数获取失败");
		}
		${className} ${humpName} = new ${className}();
		try {
			${humpName} = ${humpName}Dao.GetByKey(Integer.valueOf(id));
		} catch (Exception e) {
			logger.error("删除id>>" + id + "异常", e);
			return R.error(197, "程序异常");
		}
		R r = R.ok(200, "操作成功");
		r.putAll(JSON.parseObject(JSON.toJSONString(${humpName}), HashMap.class));
		return r;
	}

	public ${className} getById(String id) {
		return ${humpName}Dao.GetByKey(Integer.valueOf(id));
	}

    public List<${className}> list(${className} ${humpName}){
    	return ${humpName}Dao.list(${humpName});
    }

    public List<Map<String,Object>> listMap(Map<String,Object> param){
    	return ${humpName}Dao.listMap(param);
    }


}